//
// Created by lenovo on 2021/12/8.
//

struct clock {
    int hour;
    int minute;
    int second;
};
typedef struct clock CLOCK;
//然后，将下列用全局变量编写的时钟模拟显示程序改成用结构体变量类型CLOCK重新编写，不使用全局变量。
//已知用全局变量编写的时钟模拟显示程序如下：
#include  <stdio.h>
#include <time.h>
#include <stdlib.h>
void Update(struct clock *student) {
    student->second++;
    if (student->second == 60) {
        student->second = 0;
        student->minute++;
    }
    if (student->minute == 60) {
        student->minute = 0;
        student->hour++;
    }
    if (student->hour == 24) {
        student->hour = 0;
    }
}
void Display(struct clock *student) {
    printf("%2d:%2d:%2d\r", student->hour, student->minute, student->second);
}
//函数功能：模拟延迟1秒的时间
void Delay(void) {
    long t;
    for (t = 0; t < 2000000000; t++) {}
}
int Experiment7_3() {
    long i;
    time_t rawTime;
    time(&rawTime);
    struct tm *timeInfo = localtime(&rawTime);
    struct clock student;
    student.hour = timeInfo->tm_hour;    /*hour,minute,second赋初值*/
    student.minute = timeInfo->tm_min;
    student.second = timeInfo->tm_sec;
    for (i = 0; i < 100000; i++)    /*利用循环结构，控制时钟运行的时间*/
    {
        Update(&student);    /*时钟更新*/
        Display(&student);   /*时间显示*/
        Delay();/*模拟延时1秒*/
        system("cls");
    }
}